package pckDao;

import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import pckEntidad.Marca;
import pckUtil.Conexion;


public class MarcaDao implements Serializable {
private Conexion oConexion=new Conexion();
private Marca oMarca;
private List<Marca> lisMarca=null;

   public Marca obtener(String codigo) {
         String sql = "select * from tblMarca where idMarc=?";
        try {
            PreparedStatement pst = oConexion.getConnection().prepareStatement(sql);                
            pst.setString(1, codigo);
            ResultSet rst = pst.executeQuery();
            
            while(rst.next()){
                oMarca=new Marca();
                oMarca.setIdmarc(rst.getString(1));
                oMarca.setNombmar(rst.getString(2));
                oMarca.setDescmar(rst.getString(3));
            }
            return  oMarca;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
         return null;
     }
   
   
   public void insertar(Marca oMar){
         String sql = "{call sping_Marca(?,?)}";
         CallableStatement cst;
        try {
            cst = oConexion.getConnection().prepareCall(sql);
            cst.setString(1, oMar.getNombmar());
            cst.setString(2, oMar.getDescmar());
            cst.execute();            
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }   
     }
    
    
    public List<Marca> listar(){
    String sql = "{call splis_Marca}";
        try {
            CallableStatement cst = oConexion.getConnection().prepareCall(sql);
            ResultSet rst = cst.executeQuery();
            lisMarca= new ArrayList<Marca>();
            while(rst.next()){
                oMarca = new Marca();
                oMarca.setIdmarc(rst.getString(1));
                oMarca.setIdmarc(rst.getString(2));  
                oMarca.setNombmar(rst.getString(3));
                lisMarca.add(oMarca);
            }
            return  lisMarca;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
         return null;
     }
    
    
    public void actualizar( Marca oMar){
        try {
            String sql = "{call spact_tblMarca(?,?,?)}";
            CallableStatement cst = oConexion.getConnection().prepareCall(sql);
            cst.setString(1, oMar.getIdmarc());
            cst.setString(2, oMar.getNombmar());
            cst.setString(3, oMar.getDescmar());
            cst.execute();
            
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
     }
    
}
